package cn.com.landsky.alarm.service;

import cn.com.landsky.alarm.common.base.BaseEntity;
import cn.com.landsky.alarm.common.utils.ReflexUtil;
import cn.com.landsky.alarm.common.utils.StringUtil;
import cn.com.landsky.alarm.mapper.BaseMapper;
import cn.com.landsky.alarm.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/***
 ** @category 请用一句话来描述其用途...
 ** @author qing.yunhui
 ** @email: 280672161@qq.com
 ** @createTime: 2018年5月23日上午10:18:50
 **/
@Service
public class BaseServiceImpl<MODEL extends BaseEntity<KEY>, KEY> implements BaseService<MODEL, KEY> {

	@Autowired
	private BaseMapper<MODEL,KEY> baseMapper;

	@Override
	public int save(MODEL model) {
		if(null==model) {throw new NullPointerException();}
		return baseMapper.insert(model);
	}

	@Override
	public int saveBatch(List<MODEL> models) {
		if(null==models||models.size()==0) {throw new NullPointerException();}
		return baseMapper.insertBatch(models);
	}

	@Override
	public int update(MODEL model) {
		if(null==model) {throw new NullPointerException();}
		return baseMapper.update(model);
	}

	@Override
	public int deleteById(KEY id) {
		if(StringUtil.isEmptys(id)) {
			throw new NullPointerException();
		}
		return baseMapper.delete(id);
	}

	@Override
	public int deleteBatchByIds(List<KEY> ids) {
		if(StringUtil.isEmptys(ids)) {
			throw new NullPointerException();
		}
		return baseMapper.deleteBatch(ids);
	}

	@Override
	public MODEL getById(KEY id) {
		if(StringUtil.isEmptys(id)) {
			return null;
		}
		return baseMapper.getById(id);
	}

	@Override
	public int queryCount(Map<String, Object> map) {
		return baseMapper.queryCount(map);
	}

	@Override
	public List<MODEL> query(Map<String, Object> map) {
		return baseMapper.query(map);
	}

	@Override
	public List<MODEL> query(MODEL model) {
		return query(ReflexUtil.clzToMap(model));
	}
}
